package com.ctshk.rpc.payment.strategy.impl;

import com.alibaba.fastjson.JSON;
import com.ctshk.rpc.payment.dto.UnionPayConfig;
import com.ctshk.rpc.payment.dto.req.unionpay.UnionPayTransReq;
import com.ctshk.rpc.payment.req.PayCommonReq;
import com.ctshk.rpc.payment.utils.PayUtil;
import com.ctshk.rpc.payment.utils.unionpay.UnionPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @BelongsProject: ctshk-cloud
 * @BelongsPackage: com.ctshk.rpc.payment.strategy.impl
 * @Author: hef
 * @CreateTime: 2021-04-27 16:07
 * @Description: 描述
 */
@Slf4j
@Component
public class UnionPayWebStrategy extends UnionPayStrategy {
    /**
     * 请求地址
     */
    private static final String REQ_ADDR = "/gateway/api/frontTransReq.do";

    @Override
    public String pay(PayCommonReq req) throws Exception {
        UnionPayConfig config = getConfig();
        Map<String, Object> data = new UnionPayTransReq(
                "08", config.getCallbackUrl(), config.getMerId(), String.valueOf(req.getOrderId()),
                LocalDateTime.now(), req.getTotalFee(), "156", ""
        ).toMap();
        // config.getFrontUrl() + req.getOrderNumber()
        Map<String, String> submitFromData = UnionPayUtil.signData(data);
        String requestFrontUrl = config.getRequestHostUrl() + REQ_ADDR;
        log.info("【支付服务-银联支付WEB】回参url:{}, data:{}", requestFrontUrl, JSON.toJSONString(submitFromData));
//        String requestFrontUrl = "https://developer.unionpayintl.com/proxy/SecurePayt/purchase/up_1j9zzl8sm4tg_a2jp/frontTransReq.do";
        String html = PayUtil.createAutoFormHtml(requestFrontUrl, submitFromData, "UTF-8");
        log.info("【支付服务-银联支付WEB】构建html:{}", html);
        return html;
    }

}
